/**
 * 物理数据类型
 */
import PhysicalTypeNecessity from '../enums/PhysicalTypeNecessity.js';

export default class PhysicalType {

    name = '';
    lengthNecessity = PhysicalTypeNecessity.OPTIONAL;
    defaultLength = 0;
    scaleNecessity = PhysicalTypeNecessity.OPTIONAL;
    defaultScale = 0;
    /**
     * @type {[String]}
     */
    defaultValues = [];
    /**
     * @type {[String]}
     */
    defaultClasses = [];
    /**
     * @type {[String]}
     */
    usableClasses = [];

    constructor(name, lengthNecessity, defaultLength, scaleNecessity, defaultScale, defaultValues, defaultClasses, usableClasses) {
        this.name = name;
        this.lengthNecessity = lengthNecessity;
        this.defaultLength = defaultLength;
        this.scaleNecessity = scaleNecessity;
        this.defaultScale = defaultScale;
        if (Array.isArray(defaultValues)) {
            this.defaultValues.push(...defaultValues);
        }
        if (Array.isArray(defaultClasses)) {
            this.defaultClasses.push(...defaultClasses);
        }
        if (Array.isArray(usableClasses)) {
            this.usableClasses.push(...usableClasses);
        }
    }

    clone(consumer) {
        let newType = new PhysicalType(this.name, this.lengthNecessity, this.defaultLength, this.scaleNecessity,
            this.defaultScale, [...this.defaultValues], [...this.defaultClasses], [...this.usableClasses]);
        if (typeof consumer === 'function') {
            consumer(newType);
        }
        return newType;
    }

    supports(className) {
        if (!className || this.defaultClasses?.includes(className) || this.usableClasses?.includes(className)) {
            return true;
        }
        return className.wildcardMatches(...this.usableClasses);
    }

}
